import loginService  from "../../service/login/loginService";
const koaRouter = require('koa-router');
const router = koaRouter();
const addtoken = require('../../config/addtoken');  // 创建 token

// 登录
router.post('/signIn',async(ctx)=>{
	let result:any=await new loginService().signIn(ctx.request.body);
	if(result){
		if(result.msg){
			ctx.body ={success:false,msg:result.msg};
		}else{
			let token = addtoken(result);
			ctx.body ={success:true,datas:result,token};
		}
	}else{
		ctx.body ={success:false,msg:'未知错误',};
	}
})
// 注册
router.post('/register',async(ctx)=>{
	let result:any=await new loginService().register(ctx.request.body);
	if(result){
		ctx.body ={success:true,datas:result};
	}else{
		ctx.body ={success:false,msg:"未知错误"};
	}
})
// 获取用户权限
router.get('/getUserMenu',async(ctx)=>{
	// console.log(333,ctx.req.authData);
	let result:any=await new loginService().getUserMenu(ctx.request.query);
	if(result){
		ctx.body ={success:true,datas:result};
	}else{
		ctx.body ={success:false,msg:"未知错误"};
	}
})
// 根据token查询用户信息
router.get('/getUserInfoById',async(ctx)=>{
	let result:any=await new loginService().getUserInfoById(ctx.req.authData.userId);
	if(result){
		ctx.body ={success:true,datas:result};
	}else{
		ctx.body ={success:false,msg:"未知错误"};
	}
})

module.exports = router;